//
//  Utils.swift
//  DownloadTube
//
//  Created by Carlos on 09/10/14.
//  Copyright (c) 2014 Carlos. All rights reserved.
//

import UIKit
import SystemConfiguration

class Utils: NSObject {
 
    class var sharedInstance: Utils {
        struct Static {
            static var instance: Utils?
            static var token: dispatch_once_t = 0
        }
        
        dispatch_once(&Static.token) {
            Static.instance = Utils()
        }
        
        return Static.instance!
    }

    
    class func parseQueryStr(queryStr:String) -> [String:String] {
        
        var nsDictionary: [String:String]=[:]
        
        for(pair) in queryStr.componentsSeparatedByString("&") {
            
            var values:[String] = pair.componentsSeparatedByString("=")
            nsDictionary[values[0]] = values[1]
        }
        
        return nsDictionary;
    }
    
    class func colorFromRGB(red: CGFloat, green: CGFloat, blue: CGFloat) -> UIColor {
        
        return UIColor(red: red/255, green: green/255, blue: blue/255, alpha: 1.0)
    }
    
    class func loadImage(name:String, type:String) -> UIImage {
        
        let path = NSBundle.mainBundle().pathForResource(name, ofType: type)!
        return UIImage(contentsOfFile: path)!
    }
    
    class func colorWithHexString (hex:String) -> UIColor {
        
        var cString:NSString = hex.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).uppercaseString
        
        if (cString.hasPrefix("#")) {
            cString = cString.substringFromIndex(1)
        }
        
        if (cString.length != 6) {
            return UIColor.grayColor()
        }
        
        var rString = cString.substringToIndex(2)
        var gString = cString.substringWithRange(NSRange(location: 2, length: 2))
        var bString = cString.substringWithRange(NSRange(location: 4, length: 2))
        
        var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0;
        NSScanner(string: rString).scanHexInt(&r)
        NSScanner(string: gString).scanHexInt(&g)
        NSScanner(string: bString).scanHexInt(&b)
        
        return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(1))
    }
    
    class func formatSeconds (totalSeconds:Int) -> String {
        
        let seconds = totalSeconds % 60
        let minutes = (totalSeconds / 60) % 60
        let hours = totalSeconds / 3600
        
        return NSString(format: "%02d:%02d:%02d", hours, minutes, seconds)
    }
    
    class func localizedString(key: String, var arguments: [CVarArgType]) -> String {
        
        return String(format: NSLocalizedString(key, comment: ""), arguments: arguments)
    }
    
    class func localizedString(key: String) -> String {
        
        return String(format: NSLocalizedString(key, comment: ""))
    }
    
    class func isMobileDataEnabled() -> Bool{
        
        let rechability = Reachability.reachabilityForInternetConnection()
        let networkStatus = rechability.currentReachabilityStatus()
       
        return networkStatus == NetworkStatus.ReachableViaWWAN
    }
    
    struct videoQA2 {
        static let qualities:Dictionary<Int, VideoQuality> =
            [5  : VideoQuality(id: 5, desc: "Very Low Definition FLV"),
            17 : VideoQuality(id: 17, desc: "Very Low Definition 3GP", playable: true, resolution: "144p", videoBitrate: 0.05, audioBitrate: 24),
            18 : VideoQuality(id: 18, desc: "Low Definition MP4", playable: true, resolution: "360p", videoBitrate: 0.5, audioBitrate: 96),
            22 : VideoQuality(id: 22, desc: "High Definition MP4", playable: true, resolution: "720p", videoBitrate: 2.5, audioBitrate: 192),
            34 : VideoQuality(id: 34, desc: "Low Definition FLV"),
            35 : VideoQuality(id: 35, desc: "Standard Definition FLV"),
            36 : VideoQuality(id: 36, desc: "Low Definition 3GP", playable: true, resolution: "240p", videoBitrate: 0.175, audioBitrate: 36),
            37 : VideoQuality(id: 37, desc: "Full High Definition MP4", playable: true, resolution: "1080p", videoBitrate: 4.5, audioBitrate: 192),
            38 : VideoQuality(id: 38, desc: "Ultra High Definition MP4", playable: true, resolution: "3072p", videoBitrate: 4.25, audioBitrate: 192),
            43 : VideoQuality(id: 43, desc: "Low Definition WebM"),
            44 : VideoQuality(id: 44, desc: "Standard Definition WebM"),
            45 : VideoQuality(id: 45, desc: "High Definition WebM"),
            46 : VideoQuality(id: 46, desc: "Full High Definition WebM"),
            82 : VideoQuality(id: 82, desc: "Low Definition 3D MP4", playable: false, resolution: "360p", videoBitrate: 0.5, audioBitrate: 96),
            83 : VideoQuality(id: 83, desc: "Standard Definition 3D MP4", playable: false, resolution: "720p", videoBitrate: 0.5, audioBitrate: 96),
            84 : VideoQuality(id: 84, desc: "High Definition 3D MP4", playable: false, resolution: "720p", videoBitrate: 2.5, audioBitrate: 192),
            85 : VideoQuality(id: 85, desc: "Full High Definition 3D MP4", playable: false, resolution: "1080p", videoBitrate: 3.5, audioBitrate: 192),
            100 : VideoQuality(id: 100, desc: "Low Definition 3D WebM"),
            101 : VideoQuality(id: 101, desc: "Standard Definition 3D WebM"),
            102 : VideoQuality(id: 102, desc: "High Definition 3D WebM")
        ]
        }
}
